package cn.tedu.antconlonysever.contract.dao.persist.repository.impl;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.util.PageInfoToPageDataConverter;
import cn.tedu.antconlonysever.contract.dao.persist.mapper.RespiteMapper;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IRespiteRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Respite;
import cn.tedu.antconlonysever.contract.pojo.vo.RespiteListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.RespiteStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

@Slf4j
@Repository
public class RespiteRepositoryImpl implements IRespiteRepository {
    @Autowired
    private RespiteMapper respiteMapper;
    @Override
    public int insert(Respite respite) {
        log.debug("开始执行【插入缓存】的数据访问，参数：{}", respite);
        return  respiteMapper.insert(respite);
    }

    @Override
    public int insertBatch(List<Respite> respitesList) {
        log.debug("开始执行【批量插入缓存】的数据访问，参数：{}", respitesList);
        return respiteMapper.insertBatch(respitesList);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【删除缓存】的数据访问，参数：{}", id);
        return respiteMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {
        log.debug("开始执行【批量删除缓存】的数据访问，参数：{}", idList);
        return respiteMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(Respite respite) {
        log.debug("开始执行【修改缓存】的数据访问，参数：{}", respite);
        return respiteMapper.updateById(respite);
    }

    @Override
    public int count() {
        log.debug("开始执行【统计缓存的数量】的数据访问，参数：无");
        return respiteMapper.selectCount(null);
    }

    @Override
    public int countLossId(Long lossId) {
        log.debug("开始执行【统计匹配名称的缓存的数量】的数据访问，参数：{}", lossId);
        QueryWrapper<Respite> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("loss_id",lossId);
        return respiteMapper.selectCount(queryWrapper);
    }

    @Override
    public int countLossIdNotId(Long id, Long lossId) {
        log.debug("开始执行【统计匹配ID但不匹配名称的缓存的数量】的数据访问，ID：{}，名称：{}", id, lossId);
        QueryWrapper<Respite> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("loss_id",lossId)
                .ne("id",id);
        return respiteMapper.selectCount(queryWrapper);
    }

    @Override
    public RespiteStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询缓存信息】的数据访问，参数：{}", id);
        return respiteMapper.getStandardById(id);
    }

    @Override
    public PageData<RespiteListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询缓存列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<RespiteListVO> list = respiteMapper.list();
        PageInfo<RespiteListVO> pageInfo = new PageInfo<>(list);
        PageData<RespiteListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
